package com.hxgz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hxgz.dao.CommonCustdrugsMapper;
import com.hxgz.pojo.CommonCustdrugs;
import com.hxgz.service.ICommonCustdrugsService;
import com.hxgz.util.StringUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wuke
 * @since 2021-04-22
 */
@Service
public class CommonCustdrugsServiceImpl extends ServiceImpl<CommonCustdrugsMapper, CommonCustdrugs> implements ICommonCustdrugsService {

    @Autowired
    CommonCustdrugsMapper commonCustdrugsMapper;


    @Override
    @Async
    public Boolean clear() {
        CommonCustdrugs commonCustdrugs = new CommonCustdrugs();
        List<CommonCustdrugs> commonCustdrugsAll = commonCustdrugsMapper.selectList(new QueryWrapper<>(commonCustdrugs));
        for (CommonCustdrugs custdrugs : commonCustdrugsAll) {
            String drugspecifications = custdrugs.getDrugspecifications();
            if (StringUtil.isEmpty(drugspecifications) || drugspecifications.contains(":")) {
                continue;
            }
            //处理 格式
            String[] split = drugspecifications.split("\\*");
            ArrayList<String> drugspecificationList = new ArrayList<>(Arrays.asList(split));
            //被* 切割  0坐标 为 （基本剂量+剂量单位）
            String dataRst = drugspecificationList.get(0);
            //规格系数
            String doseUnit = dataRst.replaceAll("[^a-z^A-Z]", "");
            //规格单位
            String baseInfoDose = StringUtils.substringBefore(dataRst, doseUnit);
            custdrugs.setBaseInfoDose(baseInfoDose);
            custdrugs.setDoseUnit(doseUnit);
            commonCustdrugsMapper.updateById(custdrugs);
        }
        return true;
    }
}
